package com.lv.phuotbacnam.ui.activity;

import android.app.Activity;
import android.app.Dialog;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.toolbox.WindowUtils;
import com.lv.phuotbacnam.ui.Constants;
import com.lv.phuotbacnam.ui.Section;
import com.lv.phuotbacnam.ui.fragment.BaseFragment;
import com.lv.phuotbacnam.ui.navigation.NavigationBar;
import com.lv.phuotbacnam.ui.navigation.NavigationController;
import com.lv.phuotbacnam.ui.navigation.NavigationMenu;

/**
 * Base app activity
 */
public abstract class BaseActivity extends Activity implements
		NavigationController {

	/**
	 * Foreground screen
	 */
	protected BaseFragment currentFragment;

	/**
	 * Currently selected section
	 */
	protected Section currentSection;

	/**
	 * Application navigation bar
	 */
	private NavigationBar navigationBar;

	/**
	 * Application navigation menu
	 */
	private NavigationMenu navigationMenu;

	/**
	 * Dialog for progress notifications
	 */
	private Dialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		// if (getActionBar() != null) {
		// initializeNavigationBar();
		// }
	}

	public void initializeNavigationBar() {
		if (navigationBar == null) {
			navigationBar = new NavigationBar(this, R.layout.view_action_bar);
		}
		navigationBar.initialize();

		navigationBar.setLeftActionButton(R.drawable.menu,
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						getNavigationMenu().showLeftMenu();
					}
				});
		navigationBar.setRightActionButton(R.drawable.p,
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						getNavigationMenu().showRightMenu();
					}
				}, false);
	}

	public void initializeNavigationBarBack() {
		if (navigationBar == null) {
			navigationBar = new NavigationBar(this, R.layout.view_action_bar);
		}
		navigationBar.initialize();

		navigationBar.setLeftActionButton(R.drawable.back,
				new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						finish();
					}
				});

		navigationBar.setRightActionButton(R.drawable.p, null, true);

	}

	public void initializeNavigationMenu(NavigationMenu.Mode mode) {
		navigationMenu = new NavigationMenu(this, mode);
		navigationMenu.initialize();

	}

	@Override
	public void enterFullScreenMode() {
		navigationBar.hide();
	}

	@Override
	public void exitFullScreenMode() {
		navigationBar.show();
	}

	@Override
	public NavigationBar getNavigationBar() {
		return navigationBar;
	}

	@Override
	public NavigationMenu getNavigationMenu() {
		return navigationMenu;
	}

	@Override
	public <F extends BaseFragment> void navigateTo(Class<F> fragmentClass,
			Section section, Bundle arguments, boolean addToBackStack) {

		currentSection = section;
		if (currentFragment != null
				&& currentFragment.getClass().equals(fragmentClass)) {
			currentFragment.refresh(arguments);
			return;
		}

		try {
			currentFragment = fragmentClass.newInstance();
			if (arguments != null) {
				currentFragment.setArguments(arguments);
			}

			FragmentTransaction fragmentTransaction = getFragmentManager()
					.beginTransaction();
			fragmentTransaction.replace(getContainerId(), currentFragment);
			if (addToBackStack) {
				fragmentTransaction.addToBackStack(null);
			}
			fragmentTransaction.commitAllowingStateLoss();

		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	@Override
	public <A extends BaseActivity> void navigateTo(Class<A> activityClass,
			boolean isFinish, Section section, Bundle arguments) {
		Intent intent = new Intent(this, activityClass);
		if (arguments != null) {
			arguments.putSerializable(Constants.KEY_INTENT_FOR_FRAGMENT,
					section);
			intent.putExtras(arguments);
		} else {
			Bundle bundle = new Bundle();
			bundle.putSerializable(Constants.KEY_INTENT_FOR_FRAGMENT,section);
			intent.putExtras(bundle);
		}

		startActivity(intent);
		if (isFinish)
			finish();
	}

	@Override
	public void exit() {
		finish();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (currentFragment != null) {
			currentFragment.onActivityResult(requestCode, resultCode, data);
		}
	}

	@Override
	public void showProgress() {
		if (progressDialog == null) {
			progressDialog = WindowUtils.buildProgressDialog(this);
		}
		progressDialog.show();
	}

	@Override
	public void hideProgress() {
		if (progressDialog != null) {
			progressDialog.hide();
		}
	}

	@Override
	public void onBackPressed() {
		if (currentFragment == null || !currentFragment.onBackPressed()) {
			super.onBackPressed();
		}
	}

	protected abstract int getContainerId();

}
